public class MinPathSum {
    // 矩阵的最小路径和
    // https://www.nowcoder.com/practice/7d21b6be4c6b429bb92d219341c4f8bb?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3FquestionJobId%3D10%26subTabName%3Donline_coding_page
    public int minPathSum (int[][] matrix) {
        // write code here
        int n = matrix.length;
        int m = matrix[0].length;
        // dp[i][j]表示到达(i,j)位置的最小路径和
        int[][] dp = new int[n+1][m+1];
        int INF = 0x3f3f3f3f;
        for (int j = 0; j <= m; j++) {
            dp[0][j] = INF;
        }
        for (int i = 0; i <= n; i++) {
            dp[i][0] = INF;
        }
        dp[0][1] = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + matrix[i-1][j-1];
            }
        }
        return dp[n][m];
    }

}
